package sk.fiit.vppj.eventmanager.data;

import java.sql.Date;
import java.util.Collections;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import sk.fiit.vppj.eventmanager.core.DatabaseManager;
/**
 * Data class holds information about trip. Class is immutable.
 * 
 * @return instance of Organization
 */
public final class Trip {
	private Map<String, Object> data = null;
	private int id = 0;
	private final static Logger log = Logger.getLogger( Organization.class.getName() );

	public Trip(int trip_id) {
		setData( trip_id );
		this.id = trip_id;
		if( this.data == null ) {
			log.log( Level.INFO, "Selected trip is not in database! id = ?", this.id );
			throw new NullPointerException( "Trip was not set!" );
		}
	}

	private void setData(int id) {
		final String sql = "SELECT * FROM jos_event_manager_trips WHERE IDTrip = ?";

		this.data = Collections.unmodifiableMap( DatabaseManager.getInstance().getJdbcTemplate().queryForMap( sql, new Object[] { id } ) );
	}

	public String getName() {
		return this.data.get( "name" ).toString();
	}

	public String getType() {
		return this.data.get( "type" ).toString();
	}

	public String getDescription() {
		return this.data.get( "description" ).toString();
	}

	public int getPrice() {
		return Integer.parseInt( this.data.get( "price" ).toString() );
	}

	public Date getDateFrom() {
		return Date.valueOf( this.data.get( "dateFrom" ).toString() );
	}

	public Date getDateTo() {
		return Date.valueOf( this.data.get( "dateTo" ).toString() );
	}

	public int getCapacity() {
		return Integer.parseInt( this.data.get( "capacity" ).toString() );
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if( this == obj )
			return true;
		if( obj == null )
			return false;
		if( getClass() != obj.getClass() )
			return false;
		Trip other = ( Trip ) obj;
		if( id != other.id )
			return false;
		return true;
	}
}
